package com.tuozixuan;

import static com.tuozixuan.GitDemoConstants.LOCAL_DIR;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.revwalk.RevCommit;

public class GitLogDemo {

	public static void main(String[] args) throws Exception {
		
		Git git = Git.open(new File(LOCAL_DIR));
		// Iterable<RevCommit> revCommitIterable = git.log().all().call();
		Iterable<RevCommit> revCommitIterable = git.log().setMaxCount(2).call();
		revCommitIterable.forEach(revCommit -> {
			System.out.println("---------------------------------------------------------");
			System.out.println("id:" + revCommit.getId());
			System.out.println("Author:" + revCommit.getAuthorIdent().getName());
			System.out.println("Committer:" + revCommit.getCommitterIdent().getName());
			System.out.println("encodingName:" + revCommit.getEncodingName());
			System.out.println("name:" + revCommit.getName());
			System.out.println("shortMessage:" + revCommit.getShortMessage());
			System.out.println("fullMessage:" + revCommit.getFullMessage());
			
			long commitTime = revCommit.getCommitTime();
			Date commitTimeObj = new Date(commitTime * 1000);
			System.out.println("commitTime:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(commitTimeObj));
			
			System.out.println(revCommit.getName() + " \"" + revCommit.getShortMessage() + "\" " + revCommit.getAuthorIdent().getName());
		});
		
	}

}
